package com.huawo.opensdk.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FirmwareVersionUtils {

    private static final Pattern PATTERN = Pattern.compile("V(.+?)R(.+?)T(.+?)H(.+?)B(\\d+).*", Pattern.CASE_INSENSITIVE);

    public static String extractV(String str) {
        return extract(str, 1);
    }

    public static String extractR(String str) {
        return extract(str, 2);
    }

    public static String extractT(String str) {
        return extract(str, 3);
    }

    public static String extractH(String str) {
        return extract(str, 4);
    }

    public static Long extractB(String str) {
        String s = extract(str, 5);
        if (s != null) {
            return Long.parseLong(s);
        }
        return null;
    }

    private static String extract(String str, int group) {
        Matcher matcher = PATTERN.matcher(str);
        if (matcher.matches()) {
            return matcher.group(group);
        }
        return null;
    }

}